package com.xuanrui.controller;

import com.xuanrui.commons.StringUtil;
import com.xuanrui.config.TokenVO;
import com.xuanrui.config.adapay.AdapayItemConfig;
import com.xuanrui.config.adapay.AdapayUtil;
import com.xuanrui.model.Player;
import com.xuanrui.pubInter.SkException;
import com.xuanrui.service.IPlayerService;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @author afeng
 * @version 1.0
 * @date 2023/11/15 下午5:03
 */
@Controller
@RequestMapping("/auth/")
public class AuthController {
    @Autowired
    private IPlayerService playerService;
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private AdapayUtil adapayUtil;


    @RequestMapping("signature")
    public String signature (HttpServletRequest request, HttpSession session, Model model) throws SkException {
        String aid = request.getParameter("aid");   // 用户id
        String pid = request.getParameter("pid");   // 用户id
        String t = request.getParameter("t");       // 是否给自己充值
        String m = request.getParameter("m");       // 金额
        String r = request.getParameter("r");       // 钻石
        String dit = request.getParameter("dit");   // 项目
        String code = request.getParameter("code"); // 微信code
        String url= StringUtil.getFullUrl(request);
        System.out.println(url);
        Player player = playerService.getDetail(pid);

        try {
            WxJsapiSignature signature = wxMpService.switchoverTo(aid).createJsapiSignature(url);
            AdapayItemConfig config = adapayUtil.getOneAdapayConfigByAppId(aid);
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(aid, config.getWxSecret(), code);
            WxOAuth2UserInfo user = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
            System.out.println(user.getOpenid());
            TokenVO tokenVO = new TokenVO();
            tokenVO.setOpenId(user.getOpenid());
            tokenVO.setWxAppId(config.getWxAppId());
            session.setAttribute("wechatAuthorization",tokenVO);


            model.addAttribute("playerName",player.getName());
            model.addAttribute("appId",signature.getAppId());
            model.addAttribute("nonceStr",signature.getNonceStr());
            model.addAttribute("timestamp",signature.getTimestamp());
            model.addAttribute("signature",signature.getSignature());
            model.addAttribute("openId",user.getOpenid());

        } catch (Exception e) {
            e.printStackTrace();
        }


        if(StringUtil.equal("xr",dit)){
            //   湖南 - 轩睿
            return "xrPay";
        }


        return "";
    }
}
